<?php

/**
 * Each report need to extend this template for usability 
 * and follow those template method
 * <ul>
 * 		<li> view </li> 	
 * 		<li> print </li>	
 * 		<li> export </li>
 * </ul>	
 * @author Pham Trung Nguyen
 *
 */

abstract class AbsReportTemplate {
    
    /**
     * Common variables used by reports
     */
	protected $name;
	protected $report_header;	
	protected $report_footer;
					
	protected $startDate;
	protected $endDate;
	protected $month;
	protected $quarter;
		
	protected $department;
	
	/**
	 * Database handler instance	 
	 */
	protected $_db;
	
	/**
	 * The controller used for rendering HTML
	 * @var Controller
	 */
	protected $_controller;	
	protected $_print;
	
	/**
	 * Instance of report exportable
	 * @var ReportExportable
	 */
	protected $exporter;
	
	
	/**
	 * Constructor 
	 * @param $name	Report name
	 */
	function __construct($name) {
		$this->name = $name;
		
		$this->header = 'element_header.html';
		$this->footer = 'element_footer.html';		
		$this->_db = Celini::dbInstance();
	}
	
	public function setDateRange($startDate, $endDate, $month = 0, $quater = 0) {
		$this->startDate = $startDate;
		$this->endDate = $endDate;
		$this->month = $month;
		$this->quarter = $quater;		
	}

	/**
	 * Query database to get data for report modeling.
	 * 
	 * The report model could be embedded in report (by variables), but if
	 * the report need to export to other format, it should be bring into
	 * independent object for other exporters.
	 * 
	 * @return The object representing report model
	 */
	abstract protected function buildReportModel();
	
	/**
	 * Render the report to HTML format
	 * @return HTML
	 */
	abstract protected function renderReportHtml();
	
	/**
	 * This is a template method
	 * Build model then render report to HTML format
	 * 
	 * @param Controller $controller
	 * @param boolean $print
	 * @return HTML
	 */
	public function viewReport($controller, $print = false) {
	    $this->_controller = $controller;
	    $this->_print = $print;		
	    
		$this->buildReportModel();		
		return $this->renderReportHtml();
	}	
	
	/**
	 * Export report to specified file format
	 * 
	 * @return File sent to client
	 */
	public function export() {
    	if (!($this->exporter instanceof IReportExportable)) {
	        throw new ReportException("The exporter <$this->exporter> is not instance of IReportExportable");
	    }	    
	    $reportModel = $this->buildReportModel();
	    return $this->exporter->export($reportModel); 
	}

	public function setExporter($exporter) {	    
	    $this->exporter = $exporter;
	}
	
	
	/**
	 * Set extra data to report
	 * @param $department
	 */
	public function setDepartment($department){
		$this->department = $department;
	}

	
	////////// DATE ///////////////
	public function getDate(){
		if ($this->month != 0){			
			// parse this variable to get month and year
			$dateArr = explode('-', $this->month);
			$monthArr = explode('/', $dateArr[0]);
			return Utils::getMonth($monthArr[1])." ".L10nHelper::_("year %s",$monthArr[2]);
		} else if ($this->quarter != 0){
			$dateArr = explode('-', $this->quarter);
			$quarterArr = explode('/', $dateArr[0]);
			return L10nHelper::_("Quarter")." ".ceil($quarterArr[1]/3)." ".L10nHelper::_("quarteryear %s",$quarterArr[2]);
		} else {
			$start = explode('-',$this->startDate);
			$end = explode('-', $this->endDate);
			if ($start == $end) {
				 return L10nHelper::renderDateTime($this->startDate);
			}			
			return L10nHelper::renderDateTime($this->startDate).' - '.L10nHelper::renderDateTime($this->endDate);
		}
	}
}
?>